from flask_login import login_required
from main import app, db
from script.utils.request_utils import *
from script.utils.page_utils import get_webpage_title, get_page_icon
from script.api.general_code import *
from script.model.Tag import *
from script.model.TagCategory import *

import base64


@app.route('/api/get_all_tag', methods=['GET', 'POST'])
@login_required
def get_all_tag():
    all_categories = current_user.categories
    tags_list = [x.to_map() for x in all_categories]
    return get_success_code_with_data("tags", tags_list)


@app.route('/api/add_tag', methods=['GET', 'POST'])
@login_required
def add_tag():
    state, url = get_form_need("url")
    if state:
        url = base64.b64decode(url).decode("utf-8")
        category_id_state, category_id = has_form_and_get("category_id", default_value=current_user.user_default_category_id)
        # 检查category_id 安全性
        if not check_category_safe(category_id):
            category_id = current_user.user_default_category_id

        name_state, name = has_form_and_get("name", url)
        if not name_state:
            name = get_webpage_title(url)

        icon_url = get_page_icon(url)
        new_tag = Tag(user=current_user, tag_name=name, tag_url=url, tag_category_id=category_id, tag_icon=icon_url)
        db.session.add(new_tag)
        db.session.commit()
        return TAG_ADD_SUCCESS


@app.route('/api/del_tag', methods=['GET', 'POST'])
@login_required
def del_tag():
    state, tag_id = get_form_need("tag_id")
    if state:
        tag = Tag.query.filter_by(id=tag_id, user=current_user).first()
        if tag:
            db.session.delete(tag)
            db.session.commit()
            return TAG_DEL_SUCCESS
        else:
            return TAG_DEL_FAIL


@app.route('/api/add_category', methods=['GET', 'POST'])
@login_required
def add_category():
    state, category_name = get_form_need("name")
    if state:
        if TagCategory.query.filter_by(category_name=category_name, user=current_user).first():
            return CATEGORY_EXIST
        new_category = TagCategory(user=current_user, category_name=category_name)
        db.session.add(new_category)
        db.session.commit()
        return get_success_code_with_data('category', new_category.to_all_map())
        #return CATEGORY_ADD_SUCCESS


@app.route('/api/del_category', methods=['GET', 'POST'])
@login_required
def del_category():
    state, category_id = get_form_need("category_id")
    if state:
        category = TagCategory.query.filter_by(id=category_id, user=current_user).first()
        if category and category != current_user.user_default_category:
            # 删除前移动所有书签到默认
            for tag in category.tags:
                tag.tag_category = current_user.user_default_category

            db.session.delete(category)
            db.session.commit()
            return CATEGORY_DEL_SUCCESS
    else:
        return CATEGORY_DEL_FAIL


@app.route('/api/move_tag', methods=['GET', 'POST'])
@login_required
def move_tag():
    state, tag_id, category_id = get_form_need("tag_id", "category_id")
    # 不安全则退出
    if not check_category_safe(category_id):
        return TAG_MOVE_FAIL
    else:
        tag = Tag.query.filter_by(id=tag_id, user=current_user).first()
        if tag:
            tag.tag_category_id = category_id
            db.session.commit()
            return TAG_MOVE_SUCCESS
        else:
            return TAG_MOVE_FAIL


@app.route('/api/get_all_categories', methods=['GET', 'POST'])
@login_required
def get_all_categories():
    all_categories = current_user.categories
    categories_list = [x.to_simple_map() for x in all_categories]
    return get_success_code_with_data("categories", categories_list)


